var gloabalChatInfo = {};//全局本地缓存变量
function initChatTabList() {
    var tips = layer.msg('會話清單從本地加載成功！');
    var allChatTapInfo = getALLChatTapInfoFromLocal();
    gloabalChatInfo = allChatTapInfo;
    for (var k in allChatTapInfo) {
        if (allChatTapInfo[k].type == 'single') {
            if (allChatTapInfo[k].friendAccountId == '1') {
                continue;
            }
            createChatTab(allChatTapInfo[k], false);
            continue;
        }
    }
    //为管理员会话置顶
    for (var k in allChatTapInfo) {
        if (allChatTapInfo[k].friendAccountId == '1') {
            createChatTab(allChatTapInfo[k], false);
            continue;
        }
    }
    //消息拉倒最下面
    $(".chat-body")[0].scrollTop = $(".chat-body")[0].scrollHeight;
}

//重新加载会话
function listChatTabList() {
    $("#chat-list-tab").html('')
    var allChatTapInfo = getALLChatTapInfoFromLocal();
    for (var k in allChatTapInfo) {
        if (allChatTapInfo[k].type == 'single') {
            if (allChatTapInfo[k].friendAccountId == '1') {
                continue;
            }
            createChatTab(allChatTapInfo[k], false);
            continue;
        }
    }
    //为管理员会话置顶
    for (var k in allChatTapInfo) {
        if (allChatTapInfo[k].friendAccountId == '1') {
            createChatTab(allChatTapInfo[k], false);
            continue;
        }
    }
}

//创建一个单人聊天会话
function createChatTab(option, isAdd) {
    var html = '<a class="nav-link single-chat" id="chat-' + option.friendId + '-tab" onclick="clickChatTab(' + option.friendId + ')" data-toggle="pill" href="#chat-first" role="tab" aria-controls="chat-first" aria-selected="false" data-id="' + option.friendId + '">';
    html += '<div class="media"> ';
    html += '<div class="user-status ' + option.accountStatus + '" data-id="' + option.friendId + '"></div>';
    html += '<img class="align-self-center rounded-circle" src="' + option.picture + '" alt="User Image">';
    html += '<div class="media-body">';
    html += '<h5>' + option.username + '<span class="chat-timing">' + option.time + '</span></h5>';
    if (option.lastMsg.length > 15) {
        option.lastMsg = option.lastMsg.substr(0, 15) + '...';
    }
    html += '<p class="last-msg">' + option.lastMsg + '</p>';
    html += '</div></div></a>';
    $("#chat-list-tab").prepend(html);
    if (isAdd) {
        setChatTapInfoToLocal(option);
        gloabalChatInfo[option.friendId] = option;
    }
}

//从本地中获取所有会话信息
function getALLChatTapInfoFromLocal() {
    var allChatTapInfo = localStorage.getItem('pc_chatonline_info_' + $("#current-online-user-id").val());
    if (allChatTapInfo == null) {
        allChatTapInfo = {};
    } else {
        allChatTapInfo = JSON.parse(allChatTapInfo);
    }
    return allChatTapInfo;
}

//会话信息存入本地
function setChatTapInfoToLocal(option) {
    var chatInfo = localStorage.getItem('pc_chatonline_info_' + $("#current-online-user-id").val());
    if (chatInfo == null) {
        chatInfo = {};
    } else {
        chatInfo = JSON.parse(chatInfo);
    }
    updateDataToJson(chatInfo, option.friendId, option);
    localStorage.setItem('pc_chatonline_info_' + $("#current-online-user-id").val(), JSON.stringify(chatInfo));
}

//所有会话信息存入本地
function setAllChatTapInfoToLocal(allChatTapInfo) {
    localStorage.setItem('pc_chatonline_info_' + $("#current-online-user-id").val(), JSON.stringify(allChatTapInfo));
}

//获取当前会话信息
function getCurrentChatTab(id, type) {
    if (type == 'single') {
        return gloabalChatInfo[id];
    }
}

//设置当前会话信息
function setCurrentChatTab(id, type, curChatTabInfo) {
    if (type == 'single') {
        gloabalChatInfo[id] = curChatTabInfo;
        setChatTapInfoToLocal(curChatTabInfo);
    }
}

//发送文本消息
function sendTextMsg(content, friendId, type) {
    var time = dateFormat("YYYY-mm-dd HH:MM:SS", new Date());
    showTextMsg(content, 'check', time, 'right', $("#show-user-name").text(), $("#user-center-img").attr('data-img'));
    var curChatTabInfo = getCurrentChatTab(friendId, type);
    var msgList = curChatTabInfo.msgList;
    if (msgList == null || msgList == 'undefined') {
        msgList = [];
    }
    var msgInfo = {};
    msgInfo.content = content;
    msgInfo.type = 'text';
    msgInfo.time = time;
    msgInfo.status = 'check';
    msgInfo.show = 'right';
    msgList.push(msgInfo);
    curChatTabInfo.msgList = msgList;
    curChatTabInfo.time = time;
    curChatTabInfo.lastMsg = content;
    setCurrentChatTab(friendId, type, curChatTabInfo);
    $(".chat-body")[0].scrollTop = $(".chat-body")[0].scrollHeight
}

//发送图片消息
function sendImgMsg(content, img, friendId, type) {
    var time = dateFormat("YYYY-mm-dd HH:MM:SS", new Date());
    showImgMsg(content, img, 'check', time, 'right', $("#show-user-name").text(), $("#user-center-img").attr('data-img'));
    var curChatTabInfo = getCurrentChatTab(friendId, type);
    var msgList = curChatTabInfo.msgList;
    if (msgList == null || msgList == 'undefined') {
        msgList = [];
    }
    var msgInfo = {};
    msgInfo.content = content;
    msgInfo.type = 'img';
    msgInfo.img = img;
    msgInfo.time = time;
    msgInfo.status = 'check';
    msgInfo.show = 'right';
    msgList.push(msgInfo);
    curChatTabInfo.msgList = msgList;
    curChatTabInfo.time = time;
    curChatTabInfo.lastMsg = '[圖片]' + content;
    setCurrentChatTab(friendId, type, curChatTabInfo);
    $(".chat-body")[0].scrollTop = $(".chat-body")[0].scrollHeight
}

//发送文件消息
function sendFileMsg(content, filename, size, showImg, friendId, type) {
    var time = dateFormat("YYYY-mm-dd HH:MM:SS", new Date());
    showFileMsg(content, filename, size, showImg, 'check', time, 'right', $("#show-user-name").text(), $("#user-center-img").attr('data-img'));
    var curChatTabInfo = getCurrentChatTab(friendId, type);
    var msgList = curChatTabInfo.msgList;
    if (msgList == null || msgList == 'undefined') {
        msgList = [];
    }
    var msgInfo = {};
    msgInfo.content = content;
    msgInfo.type = 'file';
    msgInfo.filename = filename;
    msgInfo.size = size;
    msgInfo.showImg = showImg;
    msgInfo.time = time;
    msgInfo.status = 'check';
    msgInfo.show = 'right';
    msgList.push(msgInfo);
    curChatTabInfo.msgList = msgList;
    curChatTabInfo.time = time;
    curChatTabInfo.lastMsg = '[文件]' + content;
    setCurrentChatTab(friendId, type, curChatTabInfo);
    $(".chat-body")[0].scrollTop = $(".chat-body")[0].scrollHeight
}

//发送音频消息
function sendAudioMsg(content, filename, friendId, type) {
    var time = dateFormat("YYYY-mm-dd HH:MM:SS", new Date());
    showAudioMsg(content, filename, 'check', time, 'right', $("#show-user-name").text(), $("#user-center-img").attr('data-img'));
    var curChatTabInfo = getCurrentChatTab(friendId, type);
    var msgList = curChatTabInfo.msgList;
    if (msgList == null || msgList == 'undefined') {
        msgList = [];
    }
    var msgInfo = {};
    msgInfo.content = content;
    msgInfo.type = 'audio';
    msgInfo.filename = filename;
    msgInfo.time = time;
    msgInfo.status = 'check';
    msgInfo.show = 'right';
    msgList.push(msgInfo);
    curChatTabInfo.msgList = msgList;
    curChatTabInfo.time = time;
    curChatTabInfo.lastMsg = '[音樂]' + content;
    setCurrentChatTab(friendId, type, curChatTabInfo);
    $(".chat-body")[0].scrollTop = $(".chat-body")[0].scrollHeight
}

//发送视频消息
function sendVideoMsg(content, filename, friendId, type) {
    var time = dateFormat("YYYY-mm-dd HH:MM:SS", new Date());
    showVideoMsg(content, filename, 'check', time, 'right', $("#show-user-name").text(), $("#user-center-img").attr('data-img'));
    var curChatTabInfo = getCurrentChatTab(friendId, type);
    var msgList = curChatTabInfo.msgList;
    if (msgList == null || msgList == 'undefined') {
        msgList = [];
    }
    var msgInfo = {};
    msgInfo.content = content;
    msgInfo.type = 'video';
    msgInfo.filename = filename;
    msgInfo.time = time;
    msgInfo.status = 'check';
    msgInfo.show = 'right';
    msgList.push(msgInfo);
    curChatTabInfo.msgList = msgList;
    curChatTabInfo.time = time;
    curChatTabInfo.lastMsg = '[視頻]' + content;
    setCurrentChatTab(friendId, type, curChatTabInfo);
    $(".chat-body")[0].scrollTop = $(".chat-body")[0].scrollHeight
}

//显示消息
function showMsg(msg) {
    var username = $("#show-user-name").text();
    var img = $("#user-center-img").attr('data-img');
    if (msg.show == 'left') {
        username = msg.username;
        img = msg.headPic;
    }
    switch (msg.type) {
        case 'text': {
            showTextMsg(msg.content, msg.status, msg.time, msg.show, username, img);
            break;
        }
        case 'img': {
            showImgMsg(msg.content, msg.img, msg.status, msg.time, msg.show, username, img);
            break;
        }
        case 'file': {
            showFileMsg(msg.content, msg.filename, msg.size, msg.showImg, msg.status, msg.time, msg.show, username, img);
            break;
        }
        case 'audio': {
            showAudioMsg(msg.content, msg.filename, msg.status, msg.time, msg.show, username, img);
            break;
        }
        case 'video': {
            showVideoMsg(msg.content, msg.filename, msg.status, msg.time, msg.show, username, img);
            break;
        }
    }
    $('[data-toggle="tooltip"]').tooltip();
    $(".right-user-img").each(function (i, e) {
        var height = $(e).next('div.chat-message-text').height();
        $(e).css({'margin-top': (parseInt(height) - 20) + 'px'});
    });
}

//显示文本消息
function showTextMsg(content, status, time, show, sender, senderImg) {
    var html = '<div class="chat-message chat-message-' + show + '">';
    html += '<div class="chat-message-username">';
    html += '<span>  用戶名: ' + sender + '</span>';
    html += '</div>';
    if (show == 'left') {
        html += '<img class="rounded-circle" data-toggle="tooltip" title="' + sender + '" src="/photo/view?filename=' + senderImg + '" alt="Generic placeholder image" style="margin-right: 0.3rem;height: 2rem;width: 2rem;">';
    } else {
        html += '<img class="rounded-circle right-user-img" data-toggle="tooltip" title="' + sender + '" src="/photo/view?filename=' + senderImg + '" alt="Generic placeholder image" style="margin-left: 0.3rem;height: 2rem;width: 2rem;float:right;">';
    }
    html += '<div class="chat-message-text" style="max-width:256px;">';
    html += '<span>' + content + '</span>';
    html += '</div>';
    html += '<div class="chat-message-meta">';
    html += '<span>' + time + '<i class="feather icon-' + status + ' ml-2"></i></span>';
    html += '</div></div>';
    $("#chat-first").find("div.empty-screen").remove();
    $("#chat-first").append(html);


}

//显示图片消息
function showImgMsg(content, img, status, time, show, sender, senderImg) {
    var html = '<div class="chat-message chat-message-' + show + '">';
    html += '<div class="chat-message-username">';
    html += '<span>  用戶名: ' + sender + '</span>';
    html += '</div>';
    if (show == 'left') {
        html += '<img class="rounded-circle" data-toggle="tooltip" title="' + sender + '" src="/photo/view?filename=' + senderImg + '" alt="Generic placeholder image" style="margin-right: 0.3rem;height: 2rem;width: 2rem;">';
    } else {
        html += '<img class="rounded-circle right-user-img" data-toggle="tooltip" title="' + sender + '" src="/photo/view?filename=' + senderImg + '" alt="Generic placeholder image" style="margin-left: 0.3rem;height: 2rem;width: 2rem;float:right;">';
    }
    html += '<div class="chat-message-text">';
    html += '<ul class="list-inline message-media">';
    html += '<li class="list-inline-item" id="avatar">';
    html += '<img src="/photo/view?filename=' + img + '" width="100px" height="100px" class="img-fluid" onclick="scanImg(this)">';
    html += '</li></ul>';
    html += '</div>';
    html += '<div class="chat-message-meta">';
    html += '<span>' + time + '<i class="feather icon-' + status + ' ml-2"></i></span>';
    html += '</div></div>';
    $("#chat-first").find("div.empty-screen").remove();
    $("#chat-first").append(html);
}

//显示文件消息
function showFileMsg(content, filename, size, showImg, status, time, show, sender, senderImg) {
    var html = '<div class="chat-message chat-message-' + show + '">';
    html += '<div class="chat-message-username">';
    html += '<span>  用戶名: ' + sender + '</span>';
    html += '</div>';
    if (show == 'left') {
        html += '<img class="rounded-circle" data-toggle="tooltip" title="' + sender + '" src="/photo/view?filename=' + senderImg + '" alt="Generic placeholder image" style="margin-right: 0.3rem;height: 2rem;width: 2rem;">';
    } else {
        html += '<img class="rounded-circle right-user-img" data-toggle="tooltip" title="' + sender + '" src="/photo/view?filename=' + senderImg + '" alt="Generic placeholder image" style="margin-left: 0.3rem;height: 2rem;width: 2rem;float:right;">';
    }
    html += '<div class="chat-message-text">';
    html += '<ul class="list-unstyled message-document">';
    html += '<li class="media">';
    html += '<img src="' + showImg + '" class="align-self-center img-fluid mr-2" alt="doc">';
    html += '<div class="media-body">';
    if (show == 'left') {
        html += '<h5><a style="color:#4c595f;" href="/download/download_file?filename=' + filename + '&downloadname=' + content.substring(0, content.lastIndexOf(".")) + '">' + content + '</a></h5>';
    } else {
        html += '<h5><a href="/download/download_file?filename=' + filename + '&downloadname=' + content.substring(0, content.lastIndexOf(".")) + '">' + content + '</a></h5>';
    }
    html += '<p>' + getFileSize(size) + '</p>';
    html += '</div></li></ul>';
    html += '</div>';
    html += '<div class="chat-message-meta">';
    html += '<span>' + time + '<i class="feather icon-' + status + ' ml-2"></i></span>';
    html += '</div></div>';
    $("#chat-first").find("div.empty-screen").remove();
    $("#chat-first").append(html);
}

//显示音频消息
function showAudioMsg(content, filename, status, time, show, sender, senderImg) {
    var html = '<div class="chat-message chat-message-' + show + '">';
    html += '<div class="chat-message-username">';
    html += '<span>  用戶名: ' + sender + '</span>';
    html += '</div>';
    if (show == 'left') {
        html += '<img class="rounded-circle" data-toggle="tooltip" title="' + sender + '" src="/photo/view?filename=' + senderImg + '" alt="Generic placeholder image" style="margin-right: 0.3rem;height: 2rem;width: 2rem;">';
    } else {
        html += '<img class="rounded-circle right-user-img" data-toggle="tooltip" title="' + sender + '" src="/photo/view?filename=' + senderImg + '" alt="Generic placeholder image" style="margin-left: 0.3rem;height: 2rem;width: 2rem;float:right;">';
    }
    html += '<div class="chat-message-text">';
    html += '<div class="message-audio">';
    html += '<audio controls>';
    html += '<source src="/download/download_file?filename=' + filename + '&downloadname=' + content.substring(0, content.lastIndexOf(".")) + '" type="audio/mpeg">';
    html += '</audio>';
    html += '</div>';
    html += '</div>';
    html += '<div class="chat-message-meta">';
    html += '<span>' + time + '<i class="feather icon-' + status + ' ml-2"></i></span>';
    html += '</div></div>';
    $("#chat-first").find("div.empty-screen").remove();
    $("#chat-first").append(html);
}

//显示视频消息
function showVideoMsg(content, filename, status, time, show, sender, senderImg) {
    var html = '<div class="chat-message chat-message-' + show + '">';
    html += '<div class="chat-message-username">';
    html += '<span>  用戶名: ' + sender + '</span>';
    html += '</div>';
    if (show == 'left') {
        html += '<img class="rounded-circle" data-toggle="tooltip" title="' + sender + '" src="/photo/view?filename=' + senderImg + '" alt="Generic placeholder image" style="margin-right: 0.3rem;height: 2rem;width: 2rem;">';
    } else {
        html += '<img class="rounded-circle right-user-img" data-toggle="tooltip" title="' + sender + '" src="/photo/view?filename=' + senderImg + '" alt="Generic placeholder image" style="margin-left: 0.3rem;height: 2rem;width: 2rem;float:right;">';
    }
    html += '<div class="chat-message-text">';
    html += '<div class="message-video">';
    html += '<video width="210" controls >';
    html += '<source src="/download/download_file?filename=' + filename + '&downloadname=' + content.substring(0, content.lastIndexOf(".")) + '" type="video/mp4">';
    html += '</video>';
    html += '</div>';
    html += '<span>' + content + '</span>';
    html += '</div>';
    html += '<div class="chat-message-meta">';
    html += '<span>' + time + '<i class="feather icon-' + status + ' ml-2"></i></span>';
    html += '</div></div>';
    $("#chat-first").find("div.empty-screen").remove();
    $("#chat-first").append(html);
}

//从本地缓存加载聊天信息
function loadMsgFromLocal(friendId) {
    var curChatTabInfo = gloabalChatInfo[friendId];
    var msgList = curChatTabInfo.msgList;
    if (msgList == null || msgList === 'undefined') {
        showEmptyMsg();
        return;
    }
    $("#chat-first").html('');
    //console.log(msgList);
    for (var i = 0; i < msgList.length; i++) {
        showMsg(msgList[i], friendId);
    }
}

//从本地缓存加载群聊天信息
function loadGroupMsgFromLocal(groupId) {
    var curChatTabInfo = gloabalChatInfo['group_' + groupId];
    var msgList = curChatTabInfo.msgList;
    if (msgList == null || msgList == 'undefined') {
        showEmptyMsg();
        return;
    }
    $("#chat-first").html('');
    //console.log(msgList);
    for (var i = 0; i < msgList.length; i++) {
        showMsg(msgList[i]);
    }
}

//显示空消息
function showEmptyMsg() {
    var html = '<div class="empty-screen">';
    html += '<img src="/home/images/empty-logo.png" class="img-fluid" alt="empty-logo">';
    html += '<h4 class="my-3">這裡空空如也.</h4>';
    html += '</div>';
    $("#chat-first").html(html);
}

//显示最后一条消息
function showLastMsgTips(lastMsg, friendId, type) {
    if (lastMsg.length > 15) {
        lastMsg = lastMsg.substr(0, 15) + '...';
    }
    if (type == 'single') {
        $("#chat-" + friendId + "-tab").find("p.last-msg").text(lastMsg);
        $("#chat-" + friendId + "-tab").find("span.chat-timing").text(dateFormat("HH:MM", new Date()));
    }
}

//清空消息记录
function clearMsg(id, type) {
    var allChatTapInfo = getALLChatTapInfoFromLocal();
    if (type == 'single') {
        var currentChatTap = allChatTapInfo[id];
        currentChatTap.msgList = [];
        allChatTapInfo[id] = currentChatTap;
        gloabalChatInfo[id] = currentChatTap;
        setAllChatTapInfoToLocal(allChatTapInfo);
        clickChatTab(id);
    }
    layer.msg('成功清除所有聊天緩存記錄！');
}

//搜索
function searchInfo(text) {
    var html = $("#chat-list-tab").html();
    $("#chat-list-tab").hide();
    $("#search-chat-list-tab").html('');
    $("#search-chat-list-tab").show();
    if (text == '' || text.trim() == '') {
        $("#chat-list-tab").show();
        $("#search-chat-list-tab").hide();
        return;
    }
    //首先搜索用户名
    $("#chat-list-tab>a.nav-link").each(function (i, e) {
        if ($(e).find("h5").text().search(text) != -1) {
            //表示搜索到了,高亮显示名称
            var search = $(e).clone();
            var replaced = search.find("h5").html().replace(new RegExp(text, 'ig'), '<font color="red">' + text + '</font>');
            search.find("h5").html(replaced);
            $("#search-chat-list-tab").append(search.prop('outerHTML'));
        }
    });
}

//搜索聊天记录
function searchMsgList(text) {
    var ret = new Array();
    var index = 0;
    for (var key in gloabalChatInfo) {
        var tab = gloabalChatInfo[key];
        if (tab.hasOwnProperty('msgList')) {
            var msgList = tab.msgList;
            for (var i = 0; i < msgList.length; i++) {
                if (msgList[i].content.search(text) != -1) {
                    var msg = {};
                    msg.id = tab.type == 'single' ? key : tab.groupId;
                    msg.content = msgList[i].content;
                    msg.type = tab.type;
                    ret[index++] = msg;
                }
            }
        }
    }
    return ret;
}

//获取显示文件大小
function getFileSize(size) {
    if (size < 1024) {
        return size + 'B';
    }
    if (size > 1024 && size < 1024 * 1024) {
        return (size / 1024).toFixed(2) + 'KB';
    }
    if (size > 1024 * 1024 && size < 1024 * 1024 * 1024) {
        return (size / 1024 / 1024).toFixed(2) + 'MB';
    }
    return '大小超出範圍';
}

//更新json对象
function updateDataToJson(data, k, v) {
    // 如果 val 被忽略
    if (typeof v === "undefined") {
        // 删除属性
        delete data[k];
    } else {
        // 添加 或 修改
        data[k] = v;
    }
}

function scanImg(obj){
    let realpath = obj.src; // url地址
    let img = new Image(); // Image对象

    img.onload = function () { // 重点：要用onload加载
        let max_height = $(window).height() - 200;
        let max_width = $(window).width() - 100;
        //rate1，rate2，rate3 三个比例中取最小的。
        let rate1 = max_height / img.height;
        let rate2 = max_width / img.width;
        let rate3 = 1;
        let rate = Math.min(rate1, rate2, rate3);
        //等比例缩放
        let imgHeight = img.height * rate; //获取图片高度
        let imgWidth = img.width * rate; //获取图片宽度
        let imgHtml = "<img src='" + realpath + "' width='" + imgWidth + "px' height='" + imgHeight + "px'/>";
        layer.open({
            type:1, // 类型是1
            shade:0.2,
            anim: 2,
            offset:'auto',
            area: ['auto','auto'],
            shadeClose:true,
            scrollbar: false,
            title: false, // 这里不要写文字
            content:imgHtml,
        });
    };
    img.src = realpath;
}